/**
 * Created by Limit on 2017/9/7.
 */
$(function () {
    $('#demo').jstree({
        'core': {
            'data': {
                'url': function (node) {
                    return "/hadoop-online/hadoop/getHadoopCatalog";
                },
                'data': function (node) {
                    return { 'id': node.id };
                }
            },
            "check_callback" : function (operation, node, parent, name) {
                node.text=name;
                operateNode(operation,node);
                return node.id;
            }
        },
        "plugins" : ["contextmenu","search"]
});
    $("#file-search").submit(function(e) {
        e.preventDefault();
        $("#demo").jstree(true).search($("#file-search-input").val());
    });
});

function operateNode(operation,node) {
    switch (operation){
        case "rename_node":
            createNode(node);
            break;
            
        case "delete_node":
            deleteNode(node);
            break;
            
    }
}

function createNode(node) {


    var url="/hadoop-online/hadoop/createHadoopFolder";
    var parent=node.parent;
    if(parent==="#")
        parent="/";
    $.ajax({
        async:false,
        url:url,
        dataType:'json',
        data:{
            id:parent+node.text,
            parent:parent
        },
        success:function (data) {
            node=data;
        }
    });
}

function deleteNode(node) {
    var url="/hadoop-online/hadoop/deleteHadoopFolder";
    var parent=node.parent;
    if(parent==="#")
        parent="/";
    $.ajax({
        async:false,
        url:url,
        dataType:'json',
        data:{
            id:node.id,
            parent:parent
        },
        success:function (data) {
            console.log(data);
        }
    });
}